<?php

namespace app\controller;

use think\facade\Request;
use app\BaseController;
use think\facade\Db;
use think\Response;
use think\response\Json;

class Management extends BaseController
{
    public function getAllUserWaitData()
    {
        try {
            // 查询 userwaitdata 表中的所有数据
            $userWaitData = Db::name('userwaitdata')->select();

            // 返回查询结果
            return json([
                'status' => true,
                'message' => '查询成功',
                'data' => $userWaitData
            ]);
        } catch (\Exception $e) {
            // 异常处理
            return json([
                'status' => false,
                'message' => $e->getMessage()
            ]);
        }
    }
    public function deleteFromUserWaitData()
    {
        $data = Request::post();
        $input_userid = $data['userid'];
        $result = $this->deleteRow('userwaitdata', $input_userid);
        return $result;
    }
    public function deleteFromUserData()
    {
        $data = Request::post();
        $input_userid = $data['userid'];
        $result = $this->deleteRow('userdata', $input_userid);
        return $result;
    }
    public function deleteRow($tableName, $id)
    {

        // 使用 Db::table() 进行删除操作
        $result = Db::table($tableName)->where('id', $id)->delete();

        if ($result) {
            return json([
                'status' => true,
                'message' => '删除成功',
            ]);
        } else {
            return json([
                'status' => false,
                'message' => '删除失败',
            ]);
        }
    }

    public function acceptUser()
    {
        // 获取请求数据中的用户ID
        $data = Request::post();
        $id = $data['userid'];

        // 获取指定ID的记录
        $record = Db::table('userwaitdata')->where('id', $id)->find();

        if ($record) {
            // 将记录插入到userdata表中
            $insertResult = Db::table('userdata')->insert($record);

            if ($insertResult) {
                // 从userwaitdata表中删除该记录
                $deleteResult = Db::table('userwaitdata')->where('id', $id)->delete();

                if ($deleteResult) {
                    return json(['status' => true, 'message' => '移动成功']);
                } else {
                    // 如果删除失败，则删除刚插入的记录
                    Db::table('userdata')->where('id', $id)->delete();
                    return json(['status' => true, 'message' => '删除失败']);
                }
            } else {
                return json(['status' => true, 'message' => '插入失败']);
            }
        } else {
            return json(['status' => true, 'message' => '记录不存在']);
        }
    }
    public function getAllUserData()
    {
        try {
            // 查询 userwaitdata 表中的所有数据
            $userData = Db::name('userdata')->select();

            // 返回查询结果
            return json([
                'status' => true, 'message' => '查询成功',
                'data' => $userData
            ]);
        } catch (\Exception $e) {
            // 异常处理
            return json([
                'status' => false,
                'message' => $e->getMessage()
            ]);
        }
    }
    public function getAllAdminData()
    {
        try {
            // 查询 userwaitdata 表中的所有数据
            $userData = Db::name('admindata')->select();

            // 返回查询结果
            return json([
                'status' => true, 'message' => '查询成功',
                'data' => $userData
            ]);
        } catch (\Exception $e) {
            // 异常处理
            return json([
                'status' => false,
                'message' => $e->getMessage()
            ]);
        }
    }
    public function buildAdminAccount()
    {
        // 获取请求数据中的用户ID
        $data = Request::post();
        $input_userid = $data['userid'];
        $input_username = $data['username'];
        $input_userpassword = $data['userpassword'];
        // 获取指定ID的记录
        $record = Db::table('admindata')->where('id', $input_userid)->find();

        if (!$record) {
            $data0 =
                [
                    'id' => $input_userid,
                    'name' => $input_username, 'password' =>  $input_userpassword,
                ];
            Db::name('admindata')->insert($data0);
            return json(['status' => true, 'message' => '添加成功']);
            // return $data;
        } else {
            // 用户名存在
            return json(['status' => false, 'message' => '用户已存在']);
        }
    }

    public function deleteFromAdminData()
    {
        $data = Request::post();
        $input_userid = $data['userid'];
        $result = $this->deleteRow('admindata', $input_userid);
        return $result;
    }

    public function adminLogin()
    {
        $data = Request::post();
        $input_userid = $data['username'];
        $input_userpassword = $data['userpassword'];

        // 查询数据库中的用户密码
        $db_user = Db::table('admindata')->where('id', $input_userid)->find();

        if ($db_user) {
            // 检查用户密码是否匹配
            if ($input_userpassword === $db_user['password']) {
                return json(['status' => true, 'message' => '登陆成功']);
            } else {
                return json(['status' => false, 'message' => '密码不正确']);
            }
        } else {
            // 用户名不存在
            return json(['status' => false, 'message' => '用户未找到']);
        }
    }

    public function deleteItem()
    {
        $data = Request::post();
        $input_id = $data['id'];
        $result = $this->deleteRow('baidubaike', $input_id);
        return $result;
    }
}
